<script>
export default {
  name: "loginSetting",
  data() {
    return {
      /* 表单数据对象 */
      formData: {
        username: '',
        password: ''
      },
      /* 表单规则对象 */
      formRules: {
        username:[{required:true,message:'请输入姓名',trigger:'blur'}],
        password:[{required:true,message:'请输入密码',trigger:'blur'}]
      }
    }
  },
  methods: {
    /* findUserDetail()函数的作用根据员工用户的id派发action获取当前员工详情 */
    async findUserDetail() {
      // 派发action获取用户信息
      const user = await this.$store.dispatch('employeesStore/findUserDetail', this.$route.params.id)
      // 数据回显
      this.formData = user
    },
    /* 点击确定按钮的回调函数 */
    async handleSubmit() {
      try {
        const message = await this.$store.dispatch('employeesStore/updateUserInfo', this.formData)
        this.$message.success(message)
        // 重新派发action获取用户信息
        this.$store.dispatch('user/getUserInfoAction')
        this.$router.back()
      } catch (error) {
        this.$message.error(error.message)
      }
    }
  },
  mounted() {
    /* 组件挂载完毕调用findUserDetail()派发action获取员工详情 */
    this.findUserDetail()
  }
}
</script>

<template>
  <div class="loginSetting">
    <el-form ref="form" :model="formData" :rules="formRules" label-width="100px">
      <el-form-item label="员工姓名" prop="username">
        <el-input v-model="formData.username" placeholder="员工姓名"></el-input>
      </el-form-item>
      <el-form-item label="账户密码" prop="password">
        <el-input type="password" v-model="formData.password" placeholder="账户密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-check" @click="handleSubmit">更新</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<style scoped lang="scss">
.loginSetting {
  .el-form {
    width: 20%;
  }
}
</style>
